{include file="public/nheader"}
<style type="text/css">
    #lay-tableList {
        height: auto;
    }

    .layui-table-view .layui-table[lay-size=lg] .layui-table-cell {
        height: auto;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <!-- // 默认操作按钮 -->
        <div class="layui-card-header layadmin-card-header-auto ">
            <div class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <select name="agentid" lay-filter="agentid" lay-search>
                                <option value="">选择代理商</option>
                                {foreach $agent as $v}
                                <option value="{$v.id}" {if !empty(input('get.agentid')) &&
                                    input('get.agentid')==$v.id}selected{/if}>{$v.nickname}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <select name="rider" lay-filter="agentid" lay-search>
                                <option value="">推荐师傅人员</option>
                                <option value="-1" {if !empty(input('get.rider')) &&
                                input('get.rider')==-1}selected{/if}>未分配</option>
                                {foreach $rider as $v}
                                <option value="{$v.id}" {if !empty(input('get.rider')) &&
                                    input('get.rider')==$v.id}selected{/if}>{$v.name|default='无名氏'}({$v.mobile})</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <select name="promoid" lay-filter="promoid" lay-search>
                                <option value="">所属套餐活动</option>
                                {foreach $promo as $v}
                                <option value="{$v.id}" {if !empty(input('get.promoid')) &&
                                    input('get.promoid')==$v.id}selected{/if}>{$v.title}({$v.agent_name})[{if $v.status
                                    == 1}进行中{else}结束{/if}]</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <select name="status" lay-verify="">
                                <option value="">状态</option>
                                <option value="1" {if !empty(input('get.status')) &&
                                    input('get.status')==1}selected{/if}>待使用</option>
                                <option value="2" {if !empty(input('get.status')) &&
                                    input('get.status')==2}selected{/if}>已使用</option>
                                <option value="2" {if !empty(input('get.status')) &&
                                    input('get.status')==2}selected{/if}>已作废</option>
                            </select>
                        </div>
                    </div>
                    <!-- <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <input name="title" class="layui-input" type="text" placeholder="ID搜索" />
                        </div>
                    </div> -->
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <input name="code" class="layui-input" type="text" placeholder="兑换码搜索" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <input name="nicknames" class="layui-input" type="text" placeholder="用户昵称/电话/车牌/姓名搜索" />
                        </div>
                    </div>
                    <input type="hidden" name="promoid" value="{:input('promoid/d',0)}" />
                    <div class="layui-inline">
                        <!-- // 默认搜索 -->
                        <button class="layui-btn icon-btn" lay-filter="formSearch" lay-submit>搜索</button>
                        <!-- // 删除所有 -->
                        <!-- <button class="layui-btn" onclick="delAlls('/agent/keys_fp')">批量分配</button> -->
                        <!-- // 删除所有 -->
                        <button class="layui-btn icon-btn layui-btn-danger" lay-batch="lay-tableList"
                            data-url="/agent/keys_status?status=3">批量作废
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <!-- // 创建数据实例 -->
        <table id="lay-tableList" lay-filter="lay-tableList"></table>
    </div>
</div>
<!-- // 列表工具栏 -->
<script type="text/html" id="tableBar">

    {{#  if(d.status ==1){ }}
        {{# if(d.rider == 0) { }}
        <a class="layui-table-text" onclick="delAll('/agent/keys_fp','{{d.id}}')">绑定</a>
        <div class="layui-divider layui-divider-vertical"></div>
        {{#  } }}
    <a class="layui-table-text" lay-open data-title="查看二维码" data-area="400px,750px" data-url="/agent/key_alert?id={{d.id}}" >查看二维码</a>
    <div class="layui-divider layui-divider-vertical"></div>
    <a class="layui-table-text" style="color:#FF5722" data-url="/agent/keys_status?id={{d.id}}&status=3&type=1" data-reload="self" data-confirm="" lay-ajax="">作废兑换码</a>
    {{# }if(d.status ==2) { }}
        <a class="layui-table-text" lay-open data-title="领取详情" data-area="80%,90%" data-url="/user/coupon?keyid={{d.id}}" >领取详情</a>
    {{# }if(d.status ==3) { }}
        已作废
    {{#  } }}
</script>
<script type="text/html" id="tableUser">
    {{#  if(d.user_id != 0){ }}
    微信信息:{{d.users.nickname}}({{d.users.bmobile}})<br/>
    洗车信息:{{d.users.name}}/{{d.users.mobile}}/{{d.users.cards}}
    {{#  }if(d.user_id == 0) { }}
    无
    {{#  } }}
</script>
<script type="text/html" id="tableRider">
    {{#  if(d.rider != 0){ }}
    {{d.riders.name}}/{{d.riders.mobile}}<br/>
    {{#  }if(d.rider == 0) { }}
    无
    {{#  } }}
</script>
{include file="public/nfooter"}
<script>
    function delAll(url, id) {
        var ids = id;
        layer.prompt({ title: '填写推荐师傅账号,已绑定将跳过,请筛选后分配' }, function (value, index, elem) {
            //捉到所有被选中的，发异步进行删除
            $.post(url, { id: ids, value: value },
                function (data) {
                    if (data.code == 200) {
                        layer.msg(data.msg, function () {
                            // 获得frame索引
                            location.reload();
                            layer.close();
                        });
                    } else {
                        layer.msg(data.msg, 'error');
                    }
                }, "json"
            ).error(function () {
                layer.msg('请求错误,请重试', 'error');
            });
        });
    }
    function delAlls(url) {
        var ids = [];
        // 获取选中的id 
        var table = layui.table;        // 表格
        var checkStatus = table.checkStatus('lay-tableList'); //idTest 即为基础参数 id 对应的值
        $.each(checkStatus.data, function (i, obj) {
            ids.push(obj.id);
        });
        layer.prompt({ title: '填写推荐师傅账号,已绑定将跳过,请筛选后绑定' }, function (value, index, elem) {
            //捉到所有被选中的，发异步进行删除
            $.post(url, { id: ids, value: value },
                function (data) {
                    if (data.code == 200) {
                        layer.msg(data.msg, function () {
                            // 获得frame索引
                            location.reload();
                            layer.close();
                        });
                    } else {
                        layer.msg(data.msg, 'error');
                    }
                }, "json"
            ).error(function () {
                layer.msg('请求错误,请重试', 'error');
            });
        });
    }
    layui.use(['admin', 'layer', 'table', 'form'], function () {
        var admin = layui.admin;
        var layer = layui.layer;
        var table = layui.table;        // 表格
        var form = layui.form;
        /*
    * 初始化表格
   */
        var isTable = table.render({
            elem: "#lay-tableList"
            , url: "{:url()}"
            , page: true
            , size: 'lg'
            , even: true
            , where: {
                promoid: "{:input('promoid/d',0)}", 'type': "{:input('type/d',0)}", 'rider': "{:input('rider/d')}"
            }
            , toolbar: '#toolbarDemo'
            , defaultToolbar: [{
                title: '导出excel,如需全部导出数据请将底部数据选择100000/页' //标题
                , layEvent: 'LAYTABLE_EXPORT' //事件名，用于 toolbar 事件中使用
                , icon: 'layui-icon-export' //图标类名
            }, {
                title: '打印页面' //标题
                , layEvent: 'LAYTABLE_PRINT' //事件名，用于 toolbar 事件中使用
                , icon: 'layui-icon-print' //图标类名
            }, {
                title: '筛选数据' //标题
                , layEvent: 'LAYTABLE_COLS' //事件名，用于 toolbar 事件中使用
                , icon: 'layui-icon-cols' //图标类名
            }]
            //            , limits: [15, 100, 500, 1000, 5000, 10000, 50000, 100000]
            , limit: 15
            , cols: [[
                { type: 'checkbox', width: 60 },
                { field: 'id', align: 'center', sort: true, width: 80, title: 'ID' },
                { field: 'title', width: 200, align: 'center', title: '活动名称' },
                { field: 'agent_name', width: 200, align: 'center', title: '持码代理' },
                { field: 'code', width: 150, align: 'center', title: '兑换码' },
                { align: 'users', width: 300, toolbar: '#tableUser', title: '用户信息' },
                { align: 'riders', width: 300, toolbar: '#tableRider', title: '师傅信息' },
                {
                    field: 'status', align: 'center', width: 120, templet: function (d) {
                        if (d.type == 1) {
                            if (d.status == 1) {
                                return '待使用';
                            }
                            if (d.status == 2) {
                                return '已使用';
                            }
                            if (d.status == 3) {
                                return '已作废';
                            }
                        } else {
                            if (d.status == 1) {
                                return '待审核';
                            }
                            if (d.status == 2) {
                                return '已发放';
                            }
                            if (d.status == 3) {
                                return '已驳回';
                            }
                        }

                        return '未知';
                    }, title: '状态'
                },
                { field: 'created', width: 200, align: 'center', title: '创建时间' },
                { align: 'center', width: 300, toolbar: '#tableBar', title: '操作' },
            ]]
        })
    })
</script>